package com.pangda.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pangda.common.constant.NoticeConstant;
import com.pangda.common.domain.PageResult;
import com.pangda.common.domain.ResponseResult;
import com.pangda.common.utils.UserContext;
import com.pangda.pojo.Activity;
import com.pangda.pojo.Notices;
import com.pangda.pojo.SysUser;
import com.pangda.pojo.dto.ActivityDto;
import com.pangda.pojo.vo.ActivityVo;
import com.pangda.service.IActivityService;
import com.pangda.service.INoticesService;
import com.pangda.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 活动表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-20
 */
@RestController
@RequestMapping("/clues/activity")
@Api(tags = "活动管理")
public class ActivityController {

    @Autowired
    private IActivityService activityService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private INoticesService noticesService;

    @GetMapping("/list")
    public PageResult<ActivityVo> list(@ApiParam("分页参数")Integer pageNum,
                                       @ApiParam("分页参数")Integer pageSize,
                                       ActivityDto activityDto) {

        Object beginCreateTime = activityDto.getParams().get("beginCreateTime");
        Object endCreateTime = activityDto.getParams().get("endCreateTime");

        Object beginTime = activityDto.getParams().get("beginTime");
        Object endTime = activityDto.getParams().get("endTime");
        LambdaQueryWrapper<Activity> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtil.isNotEmpty(activityDto.getCode()),Activity::getCode, activityDto.getCode());
        wrapper.eq(ObjectUtil.isNotEmpty(activityDto.getChannel()),Activity::getChannel, activityDto.getChannel());

        wrapper.like(ObjectUtil.isNotEmpty(activityDto.getCode()),Activity::getCode, activityDto.getCode());
        wrapper.between(ObjectUtil.isNotEmpty(beginCreateTime) && ObjectUtil.isNotEmpty(endCreateTime),Activity::getCreateTime, beginCreateTime, endCreateTime+" 23:59:59");

        wrapper.between(ObjectUtil.isNotEmpty(beginTime) && ObjectUtil.isNotEmpty(endTime),Activity::getBeginTime, beginTime, endTime+" 23:59:59");

        Page<Activity> page = activityService.page(new Page<>(pageNum, pageSize), wrapper);
        return PageResult.success(page, ActivityVo.class);


    }

    @GetMapping("/listselect/{channel}")
    public ResponseResult listselect(@PathVariable String channel) {
        Activity  activity = new Activity();
        activity.setChannel(channel);
        LambdaQueryWrapper<Activity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Activity::getChannel, channel);
        return ResponseResult.success(activityService.list(wrapper));
    }

    @PostMapping
    public ResponseResult add(@RequestBody ActivityDto activityDto) {
        String randomAlphabetic = RandomStringUtils.randomAlphabetic(8);
        activityDto.setCode(randomAlphabetic);
        activityDto.setCreateTime(String.valueOf(LocalDateTime.now()));
        Activity activity = new Activity();
        activity.setStatus(activityDto.getStatus());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        activity.setEndTime(LocalDateTime.parse(activityDto.getEndTime(), formatter));
        activity.setBeginTime(LocalDateTime.parse(activityDto.getBeginTime(),formatter));
        BeanUtil.copyProperties(activityDto, activity);
        LambdaQueryWrapper<Activity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Activity::getId, activityDto.getId());
        activityService.save(activity);
        Notices notices = new Notices();
        notices.setCreateBy(UserContext.getUser());
        notices.setNoticeType(NoticeConstant.NOTICETYPE_ANNOUNCEMENT);
        notices.setStatus(NoticeConstant.NOTICE_NEW);
        notices.setNoticeContent(activity.getCode());
        noticesService.save(new NoticesController().add(notices));
        return ResponseResult.success();
    }
    @GetMapping("/{id}")
    public ResponseResult getInfo(@PathVariable Integer id) {
        LambdaQueryWrapper<Activity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Activity::getId, id);
        return ResponseResult.success(activityService.getOne(wrapper));
    }

    @PutMapping
    public ResponseResult update(@RequestBody ActivityDto activityDto) {
        Activity activity = new Activity();
        BeanUtil.copyProperties(activityDto, activity);
        LambdaQueryWrapper<Activity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Activity::getId, activityDto.getId());
        activityService.update(activity, wrapper);
        return ResponseResult.success();
    }

    @DeleteMapping("/{ids}")
    public ResponseResult delete(@PathVariable List<String> ids) {
        activityService.removeByIds(ids);
        return ResponseResult.success();
    }
}
